<?PHP
require_once WACT_ROOT.'controller/controller.inc.php';
require_once WACT_ROOT.'controller/form.inc.php';
require_once WACT_ROOT. 'util/dataspace.inc.php';
require_once APP_ROOT.'view/common/layout.view.php';

require_once APP_ROOT.'model/common/user.php';
require_once APP_ROOT.'model/common/categories.php';
require_once APP_ROOT.'model/common/session.php';
require_once APP_ROOT.'model/common/page.php';
require_once APP_ROOT.'model/common/price.php';
require_once 'model/common/date.php';
require_once APP_ROOT . "model/common/site.php";


class PageView extends LayoutView {
	function PageView ($TemplateFile) {
		parent::LayoutView($TemplateFile);

	}

	function prepare(&$controller, &$request, &$responseModel){
		parent::prepare();
		if(!Session::isLogin())
			$this->Template->set('login',' ');

		$docbase="http://".$_SERVER['HTTP_HOST'];
		$this->Template->set('docbase',$docbase);

		if(APP_NAME!=""){
			$this->Template->set('app_name',APP_NAME);
		} else $this->Template->set('app_name','.');


		if(!Session::isLogin())
			$this->Template->set('login',' ');

		//load filters


		$page_id=$request->getParameter('id');

		$page=&Page::getRecord($page_id);
		$headline=$page->get('headline');
		$type=$page->get('type');
        $premium = $page->get('premium');
        $date_added = $page->get('date_added');
        $firstname = $page->get('firstname');
        $lastname = $page->get('lastname');
        $author_title = $page->get('author_title');

        // determine the right to see the page details
        $showdetail=false;
		if(Session::isLogin()) {
			$user_id=Session::getUserId();
			if(User::isUserSubscriptionActive($user_id)) {
				$showdetail=true;
			}
			else if(User::isUserAdmin($user_id)) {
				$showdetail=true;
			}
			if($page->get('user_id')==$user_id)	$showdetail=true;
		}

		$tz =& new MyTimeZone();


		//for interviews
		if($type==1) {

			$this->Template->set('firstname',$page->get('firstname'));
			$this->Template->set('lastname',$page->get('lastname'));
			$this->Template->set('author_title',$page->get('title'));

			$datestr=$page->get('date_added');
			$mydate=strtotime($datestr);
			$mydate = $tz->timeGMT($mydate);
			$datestr = gmstrftime('%b %d, %Y',$mydate);
			$this->Template->set('date_added',$datestr);
			$this->Template->set('headline',$headline);


			//if detail
			if($showdetail) {
				$headline='Interview - '.$headline;
				$description=$page->get('description');
				$this->Template->set('description',$description);
			// if teaser
			} else {
				$headline='Interview teaser - '.$headline;
				$teaser=$page->get('teaser');
				$this->Template->set('description',$teaser);
				//$price=&Price::getPriceByType(4);
				//$number=$price->get('price');
				//list($int,$dec)=explode('.',$number);
				//if(!($dec>0))
					//$this->Template->set('price',$int);

				$price =& Price::getPrice(4);
				$price1 =& Price::getPrice(1);
				$price2 =& Price::getPrice(2);
				$price3 =& Price::getPrice(3);

				$price = number_format($price,2);
				$price1 = number_format($price1,2);
				$price2 = number_format($price2,2);
				$price3 = number_format($price3,2);
				$this->Template->set('price',$price);
				$this->Template->set('price1',$price1);
				$this->Template->set('price2',$price2);
				$this->Template->set('price3',$price3);

				$this->Template->set('forteaser',true);
			}


		// for static page (not interview)
		} else {
			if (!$premium)  $showdetail=true;
			if($showdetail) {
				$description=$page->get('description');
				$this->Template->set('description',$description);
			} else {
                //$headline='Static page - '.$headline;
				$teaser=$page->get('teaser');
				$this->Template->set('description',$teaser);
				//$price =& Price::getPrice(4);
				//$price = number_format($price,2);
				//$this->Template->set('price',$price);
				$price =& Price::getPrice(4);
				$price1 =& Price::getPrice(1);
				$price2 =& Price::getPrice(2);
				$price3 =& Price::getPrice(3);

				$price = number_format($price,2);
				$price1 = number_format($price1,2);
				$price2 = number_format($price2,2);
				$price3 = number_format($price3,2);
				$this->Template->set('price',$price);
				$this->Template->set('price1',$price1);
				$this->Template->set('price2',$price2);
				$this->Template->set('price3',$price3);


				$this->Template->set('forteaser',true);
                		$this->Template->set('headline',$headline);
                		//$this->Template->set('firstname',$page->get('firstname'));
				//$this->Template->set('lastname',$page->get('lastname'));
				//$this->Template->set('author_title',$page->get('title'));
			}
		}

 

        
		$this->Template->set('SITE_CSS',Site::getSiteTemplateCSS());
		$this->Template->set('SITE_BASE',Site::getSiteTemplateBase());
		$this->Template->set('SITE_TITLE',Site::getTitle());
		$this->Template->set('title',Site::getTitle().' - '.$headline);



	}
}

class StaticPagePage extends PageController {
	function StaticPagePage() {
		parent::PageController();

        $mytemplate1 = 'pub/pages/page.html';

		$this->setDefaultView(new PageView($mytemplate1));

	}

}
?>
